package com.theaf.core.utils;

import com.qiniu.util.Auth;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * @Author:songwenke
 * @Description:七牛工具类
 * @Date:create in 16:10 2018/1/23
 * @Modified By:
 **/
public class QiniuUtils {
    public static final Map<String, String> PROPERTERS_MAP = new HashMap<String, String>();

    private final static String ACCESSKEY = ".access_key";
    private final static String SECRETKEY = ".secret_key";
    public final static int EXPIRES = 1800;

    static {
        ResourceBundle linkRb = ResourceBundle.getBundle("qiniu");
        Enumeration<String> keys = linkRb.getKeys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            String value = linkRb.getString(key);
            PROPERTERS_MAP.put(key, value);
        }
    }

    /**
     * 生成token
     * @param app 应用
     * @param bucketName 空间
     * @param key 文件标识，一般为空
     * @return
     */
    public static String getUploadToken(String app,String bucketName,String key){
        Auth auth = Auth.create(PROPERTERS_MAP.get(app+ACCESSKEY), PROPERTERS_MAP.get(app+SECRETKEY));
        return auth.uploadToken(bucketName,key,EXPIRES,null);
    }

    /**
     * 获取各空间的域名
     * @param bucket
     * @return
     */
    public static String getUrlByBucket(String bucket){
        return PROPERTERS_MAP.get(bucket);
    }
}
